package com.googlecode.allinclusive.fw.database;

import java.io.Serializable;

public class BaseEntity implements Serializable, Idable{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -6699020201665318036L;

	@Override
	public Serializable getId() {
		return null;
	}
	
	/**
	 * 重写equals方法
	 * 当Id相同时，2个实体对象相同
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (!BaseEntity.class.isInstance(obj)) {
			return false;
		}
		
		BaseEntity otherEntity = (BaseEntity) obj;
		if (getId() == null && otherEntity.getId() == null) {
			return true;
		} else if (getId() == null || otherEntity.getId() == null) {
			return false;
		} else {
			return getId().equals(otherEntity.getId());
		}
	}
	
	public int hashCode() {
		return getId() == null ? 17 : 17 + getId().hashCode();
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getClass().getName());
		sb.append("[id=");
		sb.append(getId());
		sb.append("]");
		return sb.toString();
	}
}
